package com.vanvalt.payhui.modules.pay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.vanvalt.payhui.modules.pay.entity.Download;
import com.vanvalt.payhui.modules.pay.service.DownloadService;

/**
 * @author Xiaoyang
 * 软件下载
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/download")
public class DownloadController extends BaseController {

	@Autowired
	private DownloadService downloadService;
	
	@ModelAttribute
	public Download get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return downloadService.get(id);
		}else{
			return new Download();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Download download, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Download> page = downloadService.findPage(new Page<Download>(request, response), download); 
        model.addAttribute("page", page);
		return "modules/pay/downloadList";
	}
	
	@RequestMapping(value = "form")
	public String form(Download download, Model model) {
		
		model.addAttribute("download", download);
		return "modules/pay/downloadForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Download download, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, download)){
			return form(download, model);
		}
		downloadService.save(download);
		addMessage(redirectAttributes, "保存软件下载信息'" + StringUtils.abbr(download.getTitle(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/download/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Download download, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		downloadService.delete(download);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"软件下载信息成功");
		return "redirect:" + adminPath + "/pay/download/?repage";
	}
	
}
